package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Charge;
import com.huike.service.ChargeService;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("charge")
public class ChargeController {

    @Autowired
    private ChargeService chargeService;

    //1.分页+多条件查询
    @RequestMapping("list")
    public ResultVO<Charge> getCharges(String chargeName,@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
                                         @RequestParam(value = "pageSize" ,defaultValue = "5") Integer pageSize ){
        PageHelper.startPage(pageNum,pageSize);
        List<Charge> list = chargeService.getCharges(chargeName);
        PageInfo<Charge> pageInfo= new PageInfo<>(list,5);
        return new ResultVO<>(pageInfo);
    }

    //查询不被删除的项目
    @RequestMapping("getChargeNoDel")
    public ResultVO<Charge> getChargeNoDel(){
        return new ResultVO(chargeService.getChargeNoDel());
    }
    @RequestMapping("updateIsDel")
    public ResultVO<Void> updateIsDel(Integer id){
        Date date = new Date();
        Integer row = chargeService.updateIsDel(id,date);
        if(row >= 1){
            return new ResultVO<Void>(200,"删除成功");
        }else {
            return new ResultVO<Void>(501,"删除失败");
        }
    }

    @RequestMapping("getChargeById")
    public Charge getChargeById(Integer id){
        return chargeService.getChargeById(id);
    }

    @RequestMapping("updateCharge")
    public ResultVO<Charge> updateCharge(Integer id, String chargeName,
                                         Double chargeMoney){
        if(chargeMoney==null || chargeMoney.equals("") ||
                chargeName==null || chargeName.equals("")){
            return new ResultVO<>(401,"有空值");
        }
        Charge charge = new Charge();
        charge.setId(id);
        charge.setChargeName(chargeName);
        charge.setChargeMoney(chargeMoney);
        charge.setChargeDate(new Date());
        Integer row = chargeService.updateCharge(charge);
        if(row>=1){
            return new ResultVO<>(200,"修改成功");
        }else{
            return new ResultVO<>(505,"修改未知错误");
        }
    }



}
